<?php
//合同订单
class SalesorderController extends Controller
{

    public function actionIndex()
    {
        $this->render('index');
    }

    public function actionGetdata(){

        $page = $_REQUEST['page'];
        $rows = $_REQUEST['rows'];
        $start = ($page-1)*$rows;
        $total = $this->connection->createCommand("select count(1) from crm_salesorder where deleted = 0 ")->queryScalar();
        $result = $this->connection->createCommand("select c.*,u.realname from crm_salesorder c left join crm_user
 u on c.customer_id = u.user_id where c.deleted = 0  limit {$rows} offset {$start}")->queryAll();

        $data = Array();
        $data['total'] = $total;
        $data['rows'] = $result;

        echo json_encode($data);
    }

    public function actionShowedit(){

        $salesorder_id = $_REQUEST['salesorder_id'];

        $salesorder = null;
        if($salesorder_id==0){
            //新增
        }else{
            //编辑
            $salesorder = $this->connection->createCommand("select c.*,u.realname from crm_salesorder c left join 
crm_user u on c.customer_id = u.user_id  where c.salesorder_id = :salesorder_id")->bindParam(':salesorder_id',$salesorder_id,PDO::PARAM_INT)->queryRow();
        }

        $this->render('showedit',['salesorder'=>$salesorder]);
    }

    public function actionSavedata(){

        $salesorder_id = $_REQUEST['salesorder_id'];
        $salesorder_name = $_REQUEST['salesorder_name'];
        $customer_id = $_REQUEST['customer_id'];
        $stage = $_REQUEST['stage'];
        $possibility = $_REQUEST['possibility'];
        $duedate = $_REQUEST['duedate'];
        $nextstep = $_REQUEST['nextstep'];

        if($salesorder_id!=null){
            //编辑
            $this->connection->createCommand("update crm_salesorder set salesorder_name = :salesorder_name,customer_id = :customer_id
,stage = :stage,possibility = :possibility,duedate = :duedate,nextstep = :nextstep
where salesorder_id = :salesorder_id")
                ->bindParam(':salesorder_id',$salesorder_id,PDO::PARAM_INT)
                ->bindParam(':salesorder_name',$salesorder_name,PDO::PARAM_STR)
                ->bindParam(':customer_id',$customer_id,PDO::PARAM_INT)
                ->bindParam(':stage',$stage,PDO::PARAM_STR)
                ->bindParam(':possibility',$possibility,PDO::PARAM_STR)
                ->bindParam(':duedate',$duedate,PDO::PARAM_STR)
                ->bindParam(':nextstep',$nextstep,PDO::PARAM_STR)
                ->execute();
        }else{
            //添加
            $this->connection->createCommand("insert into crm_salesorder (salesorder_name,customer_id,stage,possibility,duedate,nextstep) values 
(:salesorder_name,:customer_id,:stage,:possibility,:duedate,:nextstep) ")
                ->bindParam(':salesorder_name',$salesorder_name,PDO::PARAM_STR)
                ->bindParam(':customer_id',$customer_id,PDO::PARAM_INT)
                ->bindParam(':stage',$stage,PDO::PARAM_STR)
                ->bindParam(':possibility',$possibility,PDO::PARAM_STR)
                ->bindParam(':duedate',$duedate,PDO::PARAM_STR)
                ->bindParam(':nextstep',$nextstep,PDO::PARAM_STR)
                ->execute();
        }
    }


    public function actionRemove(){
        $salesorder_id = $_REQUEST['salesorder_id'];
        $this->connection->createCommand("update crm_salesorder set deleted = 1 
where salesorder_id = :salesorder_id")
            ->bindParam(':salesorder_id',$salesorder_id,PDO::PARAM_INT)
            ->execute();
    }

}